//
//  ZTabBar.swift
//  swift-weibo
//
//  Created by zero on 17/3/19.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

class ZTabBar: UITabBar {

  
    //  定义闭包
    var btnClick:((UITabBar) ->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    //  布局
    func setupUI() {
        
        addSubview(plusbtn)
        
        plusbtn.addTarget(self, action: #selector(plusBtnClick), for: .touchUpInside)
    }
    
    func plusBtnClick() {
        
        btnClick?(self)
    }
    //  布局子视图
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let btnItem_W = self.bounds.size.width / 5
        
        let btnItem_H = self.bounds.size.height
        
        var index = 0
        
        for subView in subviews{
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRect(x: CGFloat(index) * btnItem_W, y: 0, width: btnItem_W, height: btnItem_H)
                
                index += index == 1 ? 2 : 1
            }
            
        }
        
        plusbtn.center = CGPoint(x: bounds.size.width * 0.5, y: bounds.size.height * 0.5)
        plusbtn.bounds.size = CGSize(width: btnItem_W, height: btnItem_H)
        
    }
    
    
    //  懒加载
    lazy var plusbtn:UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        return btn
        
    }()

}
